﻿CREATE PROCEDURE [dbo].[yaf_user_delete](
                @UserID INT)
AS
    BEGIN
        DECLARE  @GuestUserID INT
        DECLARE  @UserName NVARCHAR(50)
        DECLARE  @GuestCount INT
        SELECT @UserName = Name
        FROM   yaf_User
        WHERE  UserID = @UserID
        SELECT TOP 1 @GuestUserID = a.UserID
        FROM   yaf_User a,
               yaf_UserGroup b,
               yaf_Group c
        WHERE  b.UserID = a.UserID
        AND b.GroupID = c.GroupID
        AND (c.Flags & 2) <> 0
        SELECT @GuestCount = COUNT(1)
        FROM   yaf_UserGroup a
               JOIN yaf_Group b
                 ON b.GroupID = a.GroupID
        WHERE  (b.Flags & 2) <> 0
        IF @GuestUserID = @UserID
           AND @GuestCount = 1
        BEGIN
            RETURN
        END
        UPDATE yaf_Message
        SET    UserName = @UserName,
               UserID = @GuestUserID
        WHERE  UserID = @UserID
        UPDATE yaf_Topic
        SET    UserName = @UserName,
               UserID = @GuestUserID
        WHERE  UserID = @UserID
        UPDATE yaf_Topic
        SET    LastUserName = @UserName,
               LastUserID = @GuestUserID
        WHERE  LastUserID = @UserID
        UPDATE yaf_Forum
        SET    LastUserName = @UserName,
               LastUserID = @GuestUserID
        WHERE  LastUserID = @UserID
        DELETE FROM yaf_Active
        WHERE       UserID = @UserID        
        DELETE FROM yaf_EventLog
        WHERE       UserID = @UserID
        DELETE FROM yaf_PMessage
        WHERE       FromUserID = @UserID
        DELETE FROM yaf_UserPMessage
        WHERE       UserID = @UserID
        DELETE FROM yaf_CheckEmail
        WHERE       UserID = @UserID
        DELETE FROM yaf_WatchTopic
        WHERE       UserID = @UserID
        DELETE FROM yaf_WatchForum
        WHERE       UserID = @UserID
        DELETE FROM yaf_UserGroup
        WHERE       UserID = @UserID
        --ABOT CHANGED
        --Delete UserForums entries Too
        DELETE FROM yaf_UserForum
        WHERE       UserID = @UserID
        --END ABOT CHANGED 09.04.2004
        DELETE FROM yaf_User
        WHERE       UserID = @UserID
    END


